package com.junko.core.base;

import com.junko.core.tool.ToolWeb;
import com.junko.model.JsonResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class BaseController {
    @Autowired
    protected HttpServletRequest request;
    protected HttpSession getSession(){
        return request.getSession();
    }
    protected String getCxt(){
        return ToolWeb.getContextPath(request);
    }
    protected void setSessionAttr(String name,Object object){
        getSession().setAttribute(name,object);
    }
    protected Object getSessionAttr(String name){
        return getSession().getAttribute(name);
    }
    protected void setAttr(String key, Object obj){
        request.setAttribute(key,obj);
    }
    protected String getPara(String name) {
        return request.getParameter(name);
    }
    protected Integer getParaToInt(String name) {
        return Integer.parseInt(getPara(name));
    }
    protected Long getParaToLong(String name) {
        return Long.parseLong(getPara(name));
    }
    protected Boolean getParaToBool(String name,boolean defaultValue) {
        Object value = getPara(name);
        if(value==null){
            return defaultValue;
        }
        return Boolean.valueOf(getPara(name));
    }
    protected Double getParaToDouble(String name) {
        return Double.parseDouble(getPara(name));
    }
    protected String getPara(String name, String defaultValue) {
        String result = getPara(name);
        return result != null && !"".equals(result)?result:defaultValue;
    }
    public BaseController keepPara() {
        Map<String, String[]> map = this.request.getParameterMap();
        for (Object o : map.entrySet()) {
            Map.Entry e = (Map.Entry) o;
            String[] values = (String[]) e.getValue();
            if (values.length == 1) {
                this.setAttr((String) e.getKey(), values[0]);
            } else {
                this.setAttr((String) e.getKey(), values);
            }
        }
        return this;
    }
    public Boolean setContainsElementInOtherSet(Set<Integer> set1, Set<Integer> set2) {
        Iterator<Integer> it = set1.iterator();
        while (it.hasNext()) {
            Object obj = it.next();
            if (set2.contains(obj)) {
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }
    public Boolean equals(String str1,String str2){
        return StringUtils.equals(str1,str2);
    }
    public Boolean equals(Integer i1,Integer i2){
        return i1-i2==0;
    }
    public Boolean equalsIgnoreCase(String str1,String str2){
        return StringUtils.equalsIgnoreCase(str1,str2);
    }
    public Boolean isEmpty(String str){
        return StringUtils.isEmpty(str);
    }
    public Boolean isNotEmpty(String str){
        return StringUtils.isNotEmpty(str);
    }

    protected JsonResult fail(){
        return JsonResult.fail("");
    }
    public JsonResult fail(String msg){
        return JsonResult.fail(msg);
    }
    protected JsonResult fail(String msg,Object data){
        return JsonResult.fail(msg,data);
    }
    protected JsonResult fail(Integer code, String msg){
        return JsonResult.fail(code,msg);
    }
    protected JsonResult fail(Integer code, String msg,Object data){
        return JsonResult.fail(code,msg,data);
    }
    protected JsonResult success(){
        return JsonResult.success();
    }
    protected JsonResult success(String msg){
        return JsonResult.success(msg);
    }
    protected JsonResult success(Object obj){
        return JsonResult.success(obj);
    }
    protected JsonResult success(String msg,Object obj){
        return JsonResult.success(200,msg,obj);
    }
    protected JsonResult success(int code,String msg,Object obj){
        return JsonResult.success(code,msg,obj);
    }
}
